﻿using LPLS.Commons.Mappers;
using LPLS.DBEntities.ConfigModels.AlarmModels;
using LPLS.DBInterface.ConfigModels.AlarmModel;
using Microsoft.IdentityModel.Tokens;
using MiniExcelLibs;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace PLCAlarmRuleConfigModule.ViewModels
{
    public  class VariableConfigViewModel : BindableBase
    {

        public DelegateCommand OpenFileCommand { get; private set; }
        private IAlarmVarableConfigService _alarmVarableConfigService;

        public VariableConfigViewModel(IAlarmVarableConfigService alarmVarableConfigService ) {


            _alarmVarableConfigService= alarmVarableConfigService;
            AlarmVarableCodeConfigs = new ObservableCollection<AlarmVarableConfig>();
             OpenFileCommand = new DelegateCommand(OpenFileDialog);
            AlarmVarableNameHeader = "变量名称";
            LoadData();
        }

        private string _AlarmVarableNameHeader;
        /// <summary>
        /// 报警变量Header
        /// </summary>
        public string AlarmVarableNameHeader
        {
            get { return _AlarmVarableNameHeader; }
            set { SetProperty(ref _AlarmVarableNameHeader, value); }
        }

        private ObservableCollection<AlarmVarableConfig> _AlarmVarableCodeConfigs;
        private string _selectedFilePath;
        public string SelectedFilePath
        {
            get => _selectedFilePath;
            set => SetProperty(ref _selectedFilePath, value);
        }
        public ObservableCollection<AlarmVarableConfig> AlarmVarableCodeConfigs
        {
            get { return _AlarmVarableCodeConfigs; }

            set { SetProperty(ref _AlarmVarableCodeConfigs, value); }
        }


        private void OpenFileDialog()
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog
            {
                Title = "选择Excel文件",
                Filter = "Excel Files|*.xlsx;*.xls|All Files|*.*"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                SelectedFilePath = openFileDialog.FileName;
                LoadExcelData(openFileDialog.FileName);
                MessageBox.Show($"选择的文件路径: {SelectedFilePath}");
            }
        }



        private  List<ExcelModel> excelModels = new List<ExcelModel>();


        private void LoadExcelData(string path) 
        
        {
         
            excelModels.Clear();
            var sheets = MiniExcel.GetSheetNames(path);
            foreach (var sheet in sheets)
            {
                Console.WriteLine($"sheet name : {sheet} ");
                var rows = MiniExcel.Query<ExcelModel>(path,  sheetName: sheet).Where(e=>!e.AlarmCode.IsNullOrEmpty());
                excelModels.AddRange(rows);///获取到了excel数据 现在需要对数据进行入库操作
                Console.WriteLine(rows);
            }

            ///获取到了Excel数据了List<AlarmVarableConfig>
            var dbdata = ReflectionMapper.MapList<ExcelModel, AlarmVarableConfig>(excelModels);

            var groupdbdata = dbdata.GroupBy(e => e.VarName);

            
            List< AlarmVarableConfig >mydbdata=new List<AlarmVarableConfig> ();
          
            foreach (var group in groupdbdata)
            {
                AlarmVarableConfig dbmodel= new AlarmVarableConfig();
                dbmodel.VarName = group.Key;

                List<AlarmVarableCodeConfig> data = new List<AlarmVarableCodeConfig>();
                Console.WriteLine($"年级: {group.Key}");
                foreach (var student in group)
                {
                    AlarmVarableCodeConfig model = new AlarmVarableCodeConfig();
                    ReflectionMapper.Map(student,model);
                    data.Add(model);

                }
                dbmodel.AlarmVarableCodeConfigs = data;
                mydbdata.Add(dbmodel);
            }
         
            _alarmVarableConfigService.InsertData(mydbdata);
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData() {

            AlarmVarableCodeConfigs.Clear();

            var mydbdata = _alarmVarableConfigService.GetData();

            AlarmVarableCodeConfigs = new ObservableCollection<AlarmVarableConfig>(mydbdata);

        }



    }
}
